package com.da.lon.wang.xlg.adapter;

import android.app.Activity;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.da.lon.wang.xlg.R;
import com.da.lon.wang.xlg.bean.WaitGoods;
import com.da.lon.wang.xlg.util.CustomListener;
import com.da.lon.wang.xlg.util.ViewHolder;
import com.da.lon.wang.xlg.view.MyListView;
import com.da.lon.wang.xlg.view.TimerTextView;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

public class OutGoodsAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private List<WaitGoods> goods;
    private Activity context;
    private ImageLoader loader;
    private CustomListener listener;

    public OutGoodsAdapter(List<WaitGoods> list, Activity context,
                           CustomListener listener) {
        // TODO Auto-generated constructor stub

        inflater = LayoutInflater.from(context);
        loader = ImageLoader.getInstance();
        this.context = context;
        this.listener = listener;
        this.goods = list;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return goods != null ? goods.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return goods.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        try {
            final WaitGoods good = goods.get(position);

            convertView = inflater.inflate(R.layout.ouka_seller_out_listviewitem,
                    null);

            TextView m_tip_left = ViewHolder.get(convertView, R.id.m_tip_left);
            TimerTextView m_tip = ViewHolder.get(convertView, R.id.m_tip);
            ImageView m_tip_img = ViewHolder.get(convertView, R.id.m_tip_img);
            TextView m_status = ViewHolder.get(convertView, R.id.m_status);
            TextView m_address = ViewHolder.get(convertView, R.id.m_address);
            LinearLayout m_layout1 = ViewHolder.get(convertView, R.id.m_layout1);
            LinearLayout m_layout2 = ViewHolder.get(convertView, R.id.m_layout2);
            MyListView m_listview = ViewHolder.get(convertView, R.id.m_listview);

            TextView m_bottom1_btn1 = ViewHolder.get(convertView,
                    R.id.m_bottom1_btn1);
            TextView m_bottom1_btn2 = ViewHolder.get(convertView,
                    R.id.m_bottom1_btn2);
            TextView m_bottom1_btn3 = ViewHolder.get(convertView,
                    R.id.m_bottom1_btn3);
            TextView m_bottom2_btn1 = ViewHolder.get(convertView,
                    R.id.m_bottom2_btn1);

            if ("待发货".equals(good.getOrders_status_n())) {
                int[] res = getTime(good.getEndbtime());

                m_tip.setTimes(res);

                if (res[0] == 0 && res[1] == 0 && res[2] == 0) {
                    m_tip.stopRun();
                } else {
                    m_tip.beginRun();
                }

                m_tip_img.setVisibility(View.VISIBLE);
                m_layout1.setVisibility(View.VISIBLE);
                m_layout2.setVisibility(View.GONE);
                m_status.setTextColor(Color.parseColor("#a6c4a8"));

                if (good.getApplydelay() == 0) {
                    m_tip_img.setVisibility(View.GONE);
                }

            } else {
                String number = good.getTrade_number();
                if (number == null || "".equals(number)) {
                    m_tip.setText("运单号：");
                } else {
                    m_tip.setText("运单号：" + number);
                }
                m_tip_left.setVisibility(View.GONE);
                m_tip_img.setVisibility(View.GONE);
                m_layout1.setVisibility(View.GONE);
                m_layout2.setVisibility(View.VISIBLE);
                m_status.setTextColor(Color.parseColor("#5E5E5E"));
            }
            m_status.setText(good.getOrders_status_n());
            m_address.setText(good.getAddress());

            OutGoodsSubAdapter adapter = new OutGoodsSubAdapter(
                    good.getOrder_product(), good, context);
            m_listview.setAdapter(adapter);

            m_bottom1_btn1.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    listener.onCustomListener(1, good, position);// 改派订单
                }
            });
            m_bottom1_btn2.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    listener.onCustomListener(2, good, position);// 手工发货
                }
            });
            m_bottom1_btn3.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    listener.onCustomListener(3, good, position);// 扫码发货
                }
            });
            m_bottom2_btn1.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    listener.onCustomListener(4, good, position);// 物流信息
                }
            });

            m_tip_img.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    listener.onCustomListener(5, good, position);// 延长发货
                }
            });

        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }

        return convertView;
    }

    private int[] getTime(long tim) {
        // TODO Auto-generated method stub
        long time = (tim - System.currentTimeMillis() / 1000);

        if (time > 0) {
            int m = (int) (time % 60);

            int fe = (int) (time / 60) % 60;
            int shi = (int) (time / 3600);

            return new int[]{shi, fe, m};
        } else {

            int m = 0;
            int fe = 0;
            int shi = 0;

            return new int[]{shi, fe, m};
        }
    }
}
